1   /*
2    * Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.
8    *
9    * This code is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12   * version 2 for more details (a copy is included in the LICENSE file that
13   * accompanied this code).
14   *
15   * You should have received a copy of the GNU General Public License version
16   * 2 along with this work; if not, write to the Free Software Foundation,
17   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18   *
19   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20   * or visit www.oracle.com if you need additional information or have any
21   * questions.
22   */
23  
24  /*
25   * @test
26   * @bug 4364833
27   * @summary PrivateCredentialPermission incorrectly canonicalizes
28   *              spaces in names
29   */
30  
31  import javax.security.auth.*;
32  
33  public class CanonError {
34  
35      public static void main(String[] args) {
36  
37          // test regular equals and implies
38          PrivateCredentialPermission pcp1 = new PrivateCredentialPermission
39                  ("a b \"pcp1\"", "read");
40          PrivateCredentialPermission pcp2 = new PrivateCredentialPermission
41                  ("a b \"pcp1\"", "read");
42          if (!pcp1.equals(pcp2) || !pcp2.equals(pcp1))
43              throw new SecurityException("CanonError test failed: #1");
44          if (!pcp1.implies(pcp2) || !pcp2.implies(pcp1))
45              throw new SecurityException("CanonError test failed: #2");
46  
47          // test equals/implies failure
48          PrivateCredentialPermission pcp3 = new PrivateCredentialPermission
49                  ("a b \"pcp3\"", "read");
50          if (pcp1.equals(pcp3) || pcp3.equals(pcp1))
51              throw new SecurityException("CanonError test failed: #3");
52          if (pcp1.implies(pcp3) || pcp3.implies(pcp1))
53              throw new SecurityException("CanonError test failed: #4");
54  
55          // test spaces in name
56          PrivateCredentialPermission pcp_4 = new PrivateCredentialPermission
57                  ("a b \"pcp 4\"", "read");
58          PrivateCredentialPermission pcp__4 = new PrivateCredentialPermission
59                  ("a b \"pcp  4\"", "read");
60          if (pcp_4.equals(pcp__4) || pcp__4.equals(pcp_4))
61              throw new SecurityException("CanonError test failed: #5");
62          if (pcp_4.implies(pcp__4) || pcp__4.implies(pcp_4))
63              throw new SecurityException("CanonError test failed: #6");
64  
65          String credClass = pcp__4.getCredentialClass();
66          System.out.println("credentialClass = " + credClass);
67          String[][] principals = pcp__4.getPrincipals();
68          if (!principals[0][1].equals("pcp  4"))
69              throw new SecurityException("CanonError test failed: #7");
70          for (int i = 0; i < principals.length; i++) {
71              for (int j = 0; j < 2; j++) {
72                  System.out.println("principals[" + i + "][" + j + "] = " +
73                                  principals[i][j]);
74              }
75          }
76  
77          credClass = pcp_4.getCredentialClass();
78          System.out.println("credentialClass = " + credClass);
79          principals = pcp_4.getPrincipals();
80          if (!principals[0][1].equals("pcp 4"))
81              throw new SecurityException("CanonError test failed: #8");
82          for (int i = 0; i < principals.length; i++) {
83              for (int j = 0; j < 2; j++) {
84                  System.out.println("principals[" + i + "][" + j + "] = " +
85                                  principals[i][j]);
86              }
87          }
88  
89          System.out.println("CanonError test passed");
90      }
91  }